package com.pcbly.web.action.login;

import java.util.ArrayList;
import java.util.List;

//import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse;

import org.ajaxanywhere.AAUtils;
 
import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.system.MenuBean;
import com.pcbly.web.bean.system.MenuChildBean;
import com.pcbly.web.bean.system.PrimarykeycnBean;
//import com.pcbly.web.bean.user.UserBean;
import com.pcbly.web.business.config.ConfigService;
import com.pcbly.web.business.menu.MenuService; 
import com.pcbly.web.components.utils.ConvertToJson; 

public class HomeAction extends BaseAction<MenuBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2647296295857704657L;
	private MenuService menuService;
	protected ConfigService configService;
	private List<MenuBean> menus = new ArrayList<MenuBean>();
	protected List<MenuChildBean> menuChildHomeBeans; 
	protected List<MenuChildBean> menuChildBeans; 
	protected int icountMenu;
	
	public int getIcountMenu() {
		return icountMenu;
	}
	public void setIcountMenu(int icountMenu) {
		this.icountMenu = icountMenu;
	}
	public void refreshLeftMenu() throws Exception {
		menuChildBeans = menuService.queryMenuChildBeansByParentId(null);
		icountMenu=menuChildBeans.size();
//		Object obj = getSession().getAttribute(Constants.LEFT_SYSTEM_MENU);
//		if (obj == null) {
//			try{
//			
//			
//			getSession().setAttribute(Constants.LEFT_SYSTEM_MENU,
//					menuChildBeans);
//			getSession().setAttribute(Constants.LEFT_SYSTEM_MENU_COUNT, menuChildBeans.size());
//			}catch(Exception e){
//				e.printStackTrace();
//			}
//		} 
//		else {
//			menuChildBeans = (List<MenuChildBean>) obj;
//		}
	}
	public List<MenuChildBean> getMenuChildBeans() {
		return menuChildBeans;
	} 

	public void setMenuChildBeans(List<MenuChildBean> menuChildBeans) {
		this.menuChildBeans = menuChildBeans;
	}
	public List<MenuChildBean> getMenuChildHomeBeans() {
		return menuChildHomeBeans;
	}

	public void setMenuChildHomeBeans(List<MenuChildBean> menuChildHomeBeans) {
		this.menuChildHomeBeans = menuChildHomeBeans;
	}

	private MenuBean menu = new MenuBean();
	private String node;
	
	private String parentId;
	
	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
 
	public String home() throws Exception {
		if (log.isDebugEnabled())
			log.debug("login home begin............");
//		menus=menuService.queryListsByUserIdAndParentId("089ED4F4-CEAD-6246-47AA-7599803D3EC5", null);
		
		
//		setJsonString(JsonUtils.object2json(menus));
		return super.home();
	}
	public void child()throws Exception{
		menuChildHomeBeans = menuService.queryMenuChildBeansByParentId(node);
		if (AAUtils.isAjaxRequest(getRequest())) {
			AAUtils.addZonesToRefresh(getRequest(), "mainChildMenu"+node);
		}
		 
	}
	public String childMenu() throws Exception {  
		menus = menuService.queryListsByParentId(node); 
		List<PrimarykeycnBean> cns=new ArrayList<PrimarykeycnBean>();
		for(MenuBean bean:menus){
			cns.add(MenuBeanToPrimarykeycn(bean)); 
		}
		setJsonString(ConvertToJson.ConverListToJson(cns));
		HttpServletResponse response = getResponse();
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			response.getWriter().write(getJsonString());
			response.getWriter().flush();
			response.getWriter().close();
			 return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace(); 
			return null;
		} 
	}
	public List<MenuChildBean> queryChildByDwr(String node) throws Exception {
		if(log.isDebugEnabled()){
			log.debug("id ......."+node);
		}
		menuChildHomeBeans = menuService.queryMenuChildBeansByParentId(node);
		return menuChildHomeBeans;
	}
	public List<MenuBean> main(String id)throws Exception{
		menus = menuService.queryListsByParentId(id); 
		return menus;
	}
	private PrimarykeycnBean MenuBeanToPrimarykeycn(MenuBean bean){
		PrimarykeycnBean cn=new PrimarykeycnBean();
		cn.setId(bean.getId());
		cn.setParentId(bean.getParent().getId());
		cn.setText(bean.getName());
		if(bean.getForward()==null){
			cn.setLinkUrl("");
		}else{
			cn.setLinkUrl(bean.getForward());
		}
		cn.setLeaf(true);
		cn.setIconCls("");
		return cn;
	}
	public String top() throws Exception {
		if (log.isDebugEnabled())
			log.debug("login top begin............");
//		UserBean user = (UserBean) getSession().getAttribute(
//				Constants.USER_SESSION_KEY);
//		if(user==null){
//			return ERROR;
//		}
//		SysConfig sysconfig=new SysConfig(configService);
//		ConfigBean bean=sysconfig.getConfigContext("");
		return SUCCESS;
	}

	public String left() throws Exception {
		if (log.isDebugEnabled())
			log.debug("login left begin............");
		try {
//			UserBean user = (UserBean) getSession().getAttribute(
//					Constants.USER_SESSION_KEY);
////			if(user==null){
////				return ERROR;
////			}else{
//			menus = menuService.queryListsByUserAndParentId(user, null);
//			HttpServletRequest request = getRequest();
//			request.setAttribute("menus", menus);
			refreshLeftMenu();
//			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		if (log.isDebugEnabled())
			log.debug("login left end............");
		return SUCCESS;
	}

	public String right() throws Exception {
		if (log.isDebugEnabled())
			log.debug("login right begin............");
		return SUCCESS;
	}
	public void setConfigService(ConfigService configService) {
		this.configService = configService;
	}
	public List<MenuBean> getMenus() {
		return menus;
	}

	public void setMenus(List<MenuBean> menus) {
		this.menus = menus;
	}

	public MenuBean getMenu() {
		return menu;
	}

	public void setMenu(MenuBean menu) {
		this.menu = menu;
	}
}
